import { CustomError } from '../error';
import debug from './debug';
import { authorize } from './middleware';
<% moduleRouters.forEach(function(moduleRouter){ %>
import <%= moduleRouter %>Router from './<%= moduleRouter %>';
<% }); %>
const publicRoute = '<%= publicRoute %>';

export default (app) => {
  // 调试 Debug
  app.use('/debug', debug);

  // 模块路由<% moduleRouters.forEach(function(moduleRouter){ %>
  app.use(`${publicRoute}/<%= moduleRouter %>`, authorize(), <%= moduleRouter %>Router);<% }); %>

  // 统一异常处理
  app.use((err, req, res, next) => {
    let code = 500;
    let category = '未知';
    let message = '服务异常';
    // 这样区分为后面统计接口报错留余地
    if (err instanceof CustomError) {
      code = err.code;
      category = err.category;
      message = err.message;
    }
    res.status(500).json({
      code,
      message: `${category}错误 | ${message}`,
    });
  });
};
